//
// This class is not used at the moment. Will be included later.
//
// Michael Liljenstam
// ISTS
//
// $Id$

package SSF.App.Worm;

import SSF.OS.NetFlow.BytesUtil;
import SSF.Util.Streams.*;
import com.renesys.raceway.SSF.*;
import java.text.*;

/** 
 * 
 * 
 * 
 * 
 */
public class WormRecorder extends BasicRecorder {
    
  private boolean DEBUG=false;
    //  private boolean DEBUG=true;
  private java.text.DecimalFormat fm = new DecimalFormat();

  private int typeCode;

  // record subtypes
  private static final byte ROUTER_STRESS_STATE_REC = 0;
  private static final byte ROUTING_STATE_REC = 1;
  private static final byte GLOBAL_STATE_REC = 2;
  private static final byte GLOBAL_ROUTING_TRAFFIC_REC = 3;

  public WormRecorder(String fileName) {
    super("SSF.Model.WormStream");
    try {
      connectWrite("file:"+fileName+".dat");
    } catch (SSF.Util.Streams.streamException e) {
      System.err.println("Can't open output stream: " + fileName);
      System.err.println(e);
      System.exit(1);
    }
    typeCode = getRecordTypeCode("SSF.Model.WormRecord");
  }

  /** From hours to seconds. */
  private static float toSecs(float t) {
    return t * 3600F;
  }

  /**
   *  Send a ROUTER_STRESS_STATE_REC record to the output stream. <br>
   *  time in hours.
   */
  public void sendRouterStressRecord(float time, int routerId, byte state) {
    byte[] rec = new byte[2];
    int startIndex = 0;

    // output
    rec[startIndex++] = ROUTER_STRESS_STATE_REC;
    rec[startIndex++] = state;

    if (DEBUG)
      System.out.println("RouterStress: " + fm.format(time)
			 + " " + routerId
			 + " state " + state);

    int source_code = getRecordSourceCode(Integer.toString(routerId));

    send(typeCode, source_code, toSecs(time), rec, 0, rec.length);
  }

  /**
   *  Send a ROUTING_STATE_REC record to the output stream. <br>
   *  time in hours.
   */
  public void sendRoutingStateRecord(float time, int routerId, 
				     byte state, int activity) {
    byte[] rec = new byte[6];
    int startIndex = 0;

    // output
    rec[startIndex++] = ROUTING_STATE_REC;
    rec[startIndex++] = state;
    startIndex = BytesUtil.intToBytes(activity, rec, startIndex);

    if (DEBUG)
      System.out.println("RoutingState: " + fm.format(time)
			 + " " + routerId
			 + " state " + state
			 + " activity " + activity);

    int source_code = getRecordSourceCode(Integer.toString(routerId));

    send(typeCode, source_code, toSecs(time), rec, 0, rec.length);
  }


  /**
   *  Send a GLOBAL_STATE_REC record to the output stream. <br>
   *  time in hours.
   */
  public void sendGlobalRecord(float time, float totalInfectedHosts) {
    byte[] rec = new byte[5];
    int startIndex = 0;

    // output
    rec[startIndex++] = GLOBAL_STATE_REC;
    startIndex = BytesUtil.floatToBytes(totalInfectedHosts, rec, startIndex);

    if (DEBUG)
      System.out.println("Globals: " + fm.format(time)
			 + " i " + totalInfectedHosts);

    send(typeCode, 0, toSecs(time), rec, 0, rec.length);
  }

  /**
   *  Send a GLOBAL_ROUTING_TRAFFIC_REC record to the output stream. <br>
   *  time in hours.
   */
  public void sendGlobalRoutingTrafficRecord(float time, 
					     int routerId,
					     int activity) {
    byte[] rec = new byte[5];
    int startIndex = 0;

    // output
    rec[startIndex++] = GLOBAL_ROUTING_TRAFFIC_REC;
    startIndex = BytesUtil.intToBytes(activity, rec, startIndex);

    if (DEBUG)
      System.out.println("Observed routing traffic: " + fm.format(time)
			 + " activity " + activity);

    int source_code = getRecordSourceCode(Integer.toString(routerId));

    send(typeCode, source_code, toSecs(time), rec, 0, rec.length);
  }

}
